<?php

namespace QyManage\Controller;

use QyManage\Controller\QyManageController;

/**
 * 企业管理-学习数据
 */
class ReportController extends QyManageController {

    //报告-所有成员
    public function userList() {
        $company_id = $this->iCompanyId;
        $userList = M()->table("qy_user_company_relat ucr")->field("u.user_id,u.user_name")
                ->join("qy_user as u using(user_id)")
                ->where(array("company_id" => $company_id, "ucr.status" => 1))
                ->order("field(ucr.role_id,1,2,0)")
                ->select();

        $this->returnList($userList);
    }

    //报告-学习情况统计（昨日/近7天）
    public function allLearnData() {
        $user_id = $this->iUserId;
        $company_id = $this->iCompanyId;
        $day_num = I("day_num", 1, "intval");
        if ($day_num == 1) {  //日报（昨日）
            $starttime = date("Y-m-d H:i:s", strtotime("yesterday"));
            $endtime = date("Y-m-d 00:00:00");
        } elseif ($day_num == 7) { //周报（7天内）
            $starttime = date("Y-m-d 00:00:00", strtotime("-7 day"));
            $endtime = date("Y-m-d 00:00:00");
        }

        
        //企业所有成员
        $arrWhereUser['ucr.company_id'] = $company_id;
        $arrWhereUser['ucr.status'] = 1;
        $userList = M()->table('qy_user_company_relat ucr')
                        ->field("ucr.user_id,ucr.role_id,u.user_name,ca.cate_name")
                        ->join("qy_user as u using(user_id)")
                        ->join("LEFT JOIN qy_category_all as ca USING(cate_id)")
                        ->where($arrWhereUser)
                        ->order("field(ucr.role_id,1,2,0)")
                        ->select();
        
        if(empty($userList)){
            $this->returnList($userList);
        }
        foreach($userList as $val){
            $uids[] = $val['user_id'];
        }
        
        //课时学习列表
        $arrWherePeriod['user_id'] = array('in', $uids);
        $arrWherePeriod['company_id'] = $company_id;
        $arrWherePeriod['create_time'] = array("between", array($starttime, $endtime));
        $PeriodRecordList = M('UserPeriodRecord')->field('user_id,count(period_id) as period_count')->where($arrWherePeriod)->group('user_id')->select();
        
        //课时任务列表
        $arrWherePaper['user_id'] = array('in', $uids);
        $arrWherePaper['company_id'] = $company_id;
        $arrWherePaper['status'] = 1;
        $arrWherePaper['create_time'] = array("between", array($starttime, $endtime));
        $PaperList = M("Paper")->field("user_id,count(paper_id) as paper_count")->where($arrWherePaper)->group('user_id')->select();
        
        $allUserData['learn_period_count'] = 0;   //学习课时总数
        $allUserData['complete_paper_count'] = 0;    //完成任务总数
        $allUserData['learn_video_count'] = 0;    //完成任务总数
        foreach ($userList as &$val) {
            $val['period_count'] = 0;    //没学习过的，表示0
            foreach ($PeriodRecordList as $vv) {
                if ($val['user_id'] == $vv['user_id']) {
                    $val['period_count'] = $vv['period_count'];
                }
            }
            
            $val['paper_count'] = 0;    //没做过任务，表示0
            foreach ($PaperList as $vv) {
                
                if ($val['user_id'] == $vv['user_id']) {
                    $val['paper_count'] = $vv['paper_count'];
                }
            }
            
            $allUserData['learn_period_count'] += $val['period_count'];
            $allUserData['complete_paper_count'] += $val['paper_count'];
            !empty($val['period_count']) && $allUserData['learn_video_count']++;
            if ($val['role_id'] == 1 || $val['role_id'] == 2){
                $val['cate_name'] = '运营，客服，美工，营销';                
            }
            
        }
        unset($val);
        $returnData['count_data'] = $allUserData;
        $returnData['user_period_list'] = $userList;
        $this->returnList($returnData);
    }

    //报告-单个成员

    public function userLearnData() {

        $user_id = I("user_id",0,"intval");
        $company_id = $this->iCompanyId;
        $day_num = I("day_num", 7, "intval");
        if ($day_num == 1) {  //日报（昨日）
            $starttime = date("Y-m-d H:i:s", strtotime("yesterday"));
            $endtime = date("Y-m-d 00:00:00");
        } elseif ($day_num == 7) { //周报（7天内）
            $starttime = date("Y-m-d 00:00:00", strtotime("-7 day"));
            $endtime = date("Y-m-d 00:00:00");
        }

        //企业用户详情
        $arrWhere['ucr.company_id'] = $company_id;
        $arrWhere['ucr.user_id']    = $user_id;
        $arrWhere['ucr.status']     = 1;
        $UserPeriodlist = M()->table("qy_user_company_relat ucr")
                ->field("ucr.user_id,u.user_name,ca.cate_name,ucr.role_id")
                ->join("LEFT JOIN qy_user as u USING(user_id)")
                ->join("LEFT JOIN qy_category_all as ca USING(cate_id)")
                ->where($arrWhere)
                ->cache(true)
                ->find();
        
        if (empty($UserPeriodlist)) {
            $this->returnList($UserPeriodlist);
        }
        //课时统计
        $where['user_id'] = $user_id;
        $where['company_id'] = $company_id;
        $where['create_time'] = array("between", array($starttime, $endtime));
        $periodCount = M("UserPeriodRecord")->where($where)->getField("count(period_id) as period_count");
        $UserPeriodlist['period_count'] = $periodCount;

        //用户做题任务数记录

        $arrPaperWhere['user_id'] = $user_id;
        $arrPaperWhere['company_id'] = $company_id;
        $arrPaperWhere['status'] = 1;
        $arrPaperWhere['create_time'] = array("between", array($starttime, $endtime));
        $PaperList = M("Paper")->field("user_id,count(paper_id) as paper_count")->where($arrPaperWhere)->find();
        $UserPeriodlist['paper_count'] = $PaperList['paper_count'];
        if(in_array($UserPeriodlist['role_id'],array(1,2))){
            $UserPeriodlist['cate_name'] = '运营，客服，美工，营销';
        }
        
        //总数据统计
        $allUserData['learn_period_count'] = $UserPeriodlist['period_count'];   //学习课时总数
        $allUserData['complete_paper_count'] = $UserPeriodlist['paper_count'];    //完成任务总数
        $returnData['count_data'] = $allUserData;
        $returnData['user_period_list'] = $UserPeriodlist;
        $this->returnList($returnData);
    }

}
